package com.file.system.controller.business;


import com.file.system.entity.res.CommonResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.file.system.tools.regular.RegularUtil;

import org.springframework.web.bind.annotation.PostMapping;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.file.system.entity.res.DataPage;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotNull;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;

import com.file.system.service.business.IBusUtensilService;
import com.file.system.entity.business.BusUtensil;

import org.springframework.web.bind.annotation.RestController;
import java.util.List;


/**
* 器具
*
* @author jch
*/
@RestController
@Validated
@RequestMapping("/bus/utensil")
public class BusUtensilController {

    @Autowired
    private IBusUtensilService service;

    /**
    * 查询器具分页
    *
    * @param pageIndex 页数
    * @param pageSize  每页条数
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/page/{pageIndex}/{pageSize}")
    public DataPage<List<BusUtensil>> page(@PathVariable
                                                          @Min(value = 0, message = "页数最小1")
                                                          Integer pageIndex,
                                                          @PathVariable
                                                          @Min(value = 1, message = "每页条数最小1")
                                                          @Max(value = 100, message = "每页条数最大100")
                                                          Integer pageSize,
                                                          @Validated BusUtensil busUtensil
        ) {
        QueryWrapper<BusUtensil> query = new QueryWrapper<>();
        query.orderByDesc("a.id");
        query.eq("a.is_del", 0);
        query.like(StrUtil.isNotEmpty(busUtensil.getSerialNumber()),"a.serial_number", busUtensil.getSerialNumber());
        query.like(StrUtil.isNotEmpty(busUtensil.getSampleName()),"a.sample_name", busUtensil.getSampleName());
        query.like(StrUtil.isNotEmpty(busUtensil.getSpecModel()),"a.spec_model", busUtensil.getSpecModel());
        query.like(StrUtil.isNotEmpty(busUtensil.getSampleFactoryNumber()),"a.sample_factory_number", busUtensil.getSampleFactoryNumber());
        query.eq(ObjectUtil.isNotNull(busUtensil.getSampleNo()),"a.sample_no", busUtensil.getSampleNo());
        query.like(StrUtil.isNotEmpty(busUtensil.getFj()),"a.fj", busUtensil.getFj());
        query.like(StrUtil.isNotEmpty(busUtensil.getDictWgsm()),"a.dict_wgsm", busUtensil.getDictWgsm());
        query.like(StrUtil.isNotEmpty(busUtensil.getSampleTechFeature()),"a.sample_tech_feature", busUtensil.getSampleTechFeature());
        query.like(StrUtil.isNotEmpty(busUtensil.getDictReportType()),"a.dict_report_type", busUtensil.getDictReportType());
        query.like(StrUtil.isNotEmpty(busUtensil.getTestingCosts()),"a.testing_costs", busUtensil.getTestingCosts());
        query.like(StrUtil.isNotEmpty(busUtensil.getDictTestDepartment()),"a.dict_test_department", busUtensil.getDictTestDepartment());
        query.like(StrUtil.isNotEmpty(busUtensil.getDescription()),"a.description", busUtensil.getDescription());
        query.eq(ObjectUtil.isNotNull(busUtensil.getNumber()),"a.number", busUtensil.getNumber());
        query.like(StrUtil.isNotEmpty(busUtensil.getDictJzzq()),"a.dict_jzzq", busUtensil.getDictJzzq());
        query.like(StrUtil.isNotEmpty(busUtensil.getDictJzpd()),"a.dict_jzpd", busUtensil.getDictJzpd());
        query.like(StrUtil.isNotEmpty(busUtensil.getOtherRequirement()),"a.other_requirement", busUtensil.getOtherRequirement());
        query.like(StrUtil.isNotEmpty(busUtensil.getReceiverOfMeasuringInstruments()),"a.receiver_of_measuring_instruments", busUtensil.getReceiverOfMeasuringInstruments());
        query.like(StrUtil.isNotEmpty(busUtensil.getCertificateRecipient()),"a.certificate_recipient", busUtensil.getCertificateRecipient());
        query.eq(ObjectUtil.isNotNull(busUtensil.getFkIndentId()),"a.fk_indent_id", busUtensil.getFkIndentId());
        pageIndex = pageIndex + 1;
        //查询集合
        DataPage<List<BusUtensil>> dp = service.page(pageIndex, pageSize, query);
        return dp;
    }

    /**
    * 通过ids查询器具集合
    *
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/listByIds")
    public CommonResult<List<BusUtensil>> listByIds(@RequestBody List<Long> ids) {
        //查询集合
        List<BusUtensil> list = service.listByIds(ids);
        return CommonResult.ok().setResult(list);
    }

    /**
    * 查询所有非删除状态器具
    *
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/listAll")
    public CommonResult<List<BusUtensil>> listAll() {
        //查询集合
        List<BusUtensil> list = service.listAll();
        return  CommonResult.ok().setResult(list);
    }


    /**
    * 通过id查询器具
    *
    * @param id 器具id
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/id/{id:" + RegularUtil.ID + "}")
    public CommonResult<BusUtensil> getById(@PathVariable
                                              @Min(value = 1, message = "id长度最小1")
                                              Long id) {
        //查询集合
        BusUtensil busUtensil = service.getById(id);
        return CommonResult.ok().setResult(busUtensil);
    }

    /**
    * 通过id删除器具
    *
    * @param id id
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/remove/{id:" + RegularUtil.ID + "}")
    public CommonResult removeById(@PathVariable
                            @Min(value = 1, message = "id长度最小1")
                            Long id) {
        //返回
        return CommonResult.check(service.removeById(id));
    }

    /**
    * 通过ids删除器具
    *
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/removeByIds")
    public CommonResult removeById(@RequestBody List<Long> ids) {
        //返回
        return CommonResult.check(service.removeByIds(ids));
    }


    /**
    * 通过id编辑器具
    *
    * @param busUtensil 器具
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/updateById")
    public CommonResult<BusUtensil> updateById(@RequestBody @Validated(BusUtensil.update.class) BusUtensil busUtensil) {
        //编辑修改
        return CommonResult.check(service.updateById(busUtensil)).setResult(busUtensil);
    }


    /**
    * 添加器具
    *
    * @param busUtensil 器具
    * @return CommonResult
    * @apiNote -
    * @author jch
    */
    @PostMapping("/save")
    public CommonResult<BusUtensil> save(@RequestBody @Validated(BusUtensil.add.class) BusUtensil busUtensil) {
        //添加器具
        return CommonResult.check(service.save(busUtensil)).setResult(busUtensil);
    }
}

